package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Document;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.Page;
import com.kaishengit.util.RowMapper;

public class DocumentDao {

	private DBHelp<Document> db = new DBHelp<Document>();
	

	public Page<Document> findByPage(Page<Document> page) {
		String sql = "select * from t_document limit ?,?";
		List<Document> list =  db.executeQueryForList(sql, new DocumentRowMapper(),page.getStartNum(),page.getPageSize());
		page.setResult(list);
		return page;
	}
	
	public List<Document> findByLimit(int start) {
		String sql = "select * from t_document order by id desc limit ?,4";
		List<Document> list =  db.executeQueryForList(sql, new DocumentRowMapper(),start);
		return list;
	}
	

	
	
	public void save(Document pro) {
		String sql = "insert into t_document(title,content) values(?,?)";
		db.executeSQL(sql, pro.getTitle(),pro.getContent());
	}
	

	
	private class DocumentRowMapper implements RowMapper<Document> {

		public Document mapperRow(ResultSet rs) throws SQLException {
			Document d = new Document();
			d.setId(rs.getInt("id"));
			d.setTitle(rs.getString("title"));
			d.setContent(rs.getString("content"));
			return d;
		}
		
	}


}
